//
//  PlayerView.m
//  ICE_Player
//
//  Created by lijinxin on 11-10-13.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "PlayerView.h"
#import "Globalize.h"
#import "PlayerViewController.h"
#import "ICE_global_var.h"
#import "BGView.h"
#include <math.h>

#define FINGER_DRAG_CONFIRM_VAL (160.f)

@interface PlayerView (PrivateMethod)
- (void)scale_to_fit_iPad;
@end

@implementation PlayerView

//Controls
@synthesize p_loop_mode_btn;
@synthesize p_play_btn;
@synthesize p_return_btn;
@synthesize p_screen_mode_btn;
@synthesize p_silent_icon;
@synthesize p_time_pos_bar;
@synthesize p_time_pos_btn;
@synthesize p_vol_pos_bar;
@synthesize p_vol_pos_btn;
@synthesize p_vol_btn;
@synthesize p_pause_state_icon;
@synthesize p_finger_drag_icon;
@synthesize p_bg_audio_logo;

//Labels
@synthesize p_movie_name_lbl;
@synthesize p_cur_time_lbl;
@synthesize p_cur_vol_lbl;
//globalize labels
@synthesize p_g_ret_lbl;
@synthesize p_g_lm_lbl;
@synthesize p_g_sm_lbl;
@synthesize p_g_ps_lbl;
@synthesize p_g_fc_lbl;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc {
	[self destroy_SubViews];
    [super dealloc];
}

- (void)create_SubViews:(UIViewController*)controller
{
    if (get_device_type() == DVC_TYPE_IPAD)
    {
        [self scale_to_fit_iPad];
    }
    
    CREATE_ICE_VIEW(p_bg_audio_logo, Player_Audio_BG_Logo, controller);
    [p_bg_audio_logo removeFromSuperview];
    [self addSubview:p_bg_audio_logo];
    //controls
    CREATE_ICE_VIEW(p_loop_mode_btn, Player_Loop_Mode_Btn, controller);
    CREATE_ICE_VIEW(p_play_btn, Player_Play_Btn, controller);
    CREATE_ICE_VIEW(p_return_btn, Player_Return_Btn, controller);
    CREATE_ICE_VIEW(p_screen_mode_btn, Player_Screen_Mode_Btn, controller);
    CREATE_ICE_VIEW(p_silent_icon, Player_Silent_Icon, controller);
    CREATE_ICE_VIEW(p_vol_btn, Player_Volume_Btn, controller);
    CREATE_ICE_VIEW(p_time_pos_bar, Player_Time_Pos_Bar, controller);
    CREATE_ICE_VIEW(p_time_pos_btn, Player_Time_Pos_Btn, controller);
    CREATE_ICE_VIEW(p_vol_pos_bar, Player_Vol_Pos_Bar, controller);
    CREATE_ICE_VIEW(p_vol_pos_btn, Player_Vol_Pos_Btn, controller);
    CREATE_ICE_VIEW(p_pause_state_icon, Player_Pause_State_Icon, controller);
    CREATE_ICE_VIEW(p_finger_drag_icon, ICE_Image_View, controller);
    CREATE_ICE_VIEW(p_movie_name_lbl, ICE_Label_View, controller);
    CREATE_ICE_VIEW(p_cur_time_lbl, ICE_Label_View, controller);
    CREATE_ICE_VIEW(p_cur_vol_lbl, ICE_Label_View, controller);
    //globalize labels
    CREATE_ICE_VIEW_GLOBALIZE(p_g_ret_lbl, ICE_Hidable_Label_View, controller, EG_LBL_ID_PLAYER_RET_BTN);
    CREATE_ICE_VIEW_GLOBALIZE(p_g_lm_lbl, ICE_Hidable_Label_View, controller, EG_LBL_ID_PLAYER_LOOP_MODE);
    CREATE_ICE_VIEW_GLOBALIZE(p_g_sm_lbl, ICE_Hidable_Label_View, controller, EG_LBL_ID_PLAYER_SCREEN_MODE);
    CREATE_ICE_VIEW_GLOBALIZE(p_g_ps_lbl, ICE_Hidable_Label_View, controller, EG_LBL_ID_PLAYER_PLAY_BTN);
    CREATE_ICE_VIEW_GLOBALIZE(p_g_fc_lbl, ICE_Hidable_Label_View, controller, EG_LBL_ID_PLAYER_PLAY_BTN);
    
    p_pause_state_icon.hidden = TRUE;
    p_bg_audio_logo.hidden = TRUE;
    is_pause_state_icon_hide = FALSE;
    is_ai_view_hide = FALSE;
    is_silent_icon_hide = FALSE;
    
    //set up controls
    
    CGRect rct;
    CGRect s_rct;
    CGPoint pt;
    //volume silder
    rct = p_vol_pos_bar.frame;
    s_rct = p_vol_pos_btn.frame;
    p_vol_pos_btn.low_y = rct.origin.y + (s_rct.size.height / 2.f);
    p_vol_pos_btn.high_y = rct.origin.y + rct.size.height - (s_rct.size.height / 2.f);
    p_vol_pos_bar.p_btn = p_vol_pos_btn;
    //time position slider
    rct = p_time_pos_bar.frame;
    s_rct = p_time_pos_btn.frame;
    p_time_pos_btn.low_x = rct.origin.x + (s_rct.size.width / 4.f);
    p_time_pos_btn.high_x = rct.origin.x + rct.size.width - (s_rct.size.width / 4.f);
    p_time_pos_bar.p_btn = p_time_pos_btn;
    
    //end of set up controls
    
    //Controls
    p_loop_mode_btn.hidden = YES;
    p_play_btn.hidden = YES;
    p_return_btn.hidden = YES;
    p_screen_mode_btn.hidden = YES;
    p_silent_icon.hidden = YES;
    p_time_pos_bar.hidden = YES;
    p_time_pos_btn.hidden = YES;
    p_vol_pos_bar.hidden = YES;
    p_vol_pos_btn.hidden = YES;
    p_vol_btn.hidden = YES;
    p_pause_state_icon.hidden = YES;
    p_finger_drag_icon.hidden = YES;
    
    //Labels
    p_movie_name_lbl.hidden = YES;
    p_cur_time_lbl.hidden = YES;
    p_cur_vol_lbl.hidden = YES;
    //globalize labels
    p_g_ret_lbl.hidden = YES;
    p_g_lm_lbl.hidden = YES;
    p_g_sm_lbl.hidden = YES;
    p_g_ps_lbl.hidden = YES;
    p_g_fc_lbl.hidden = YES;
    
    player_controller = controller;
    
    p_g_ret_lbl.text = IGS(EG_STR_ID_PLAYER_RET_BTN);
    p_g_sm_lbl.text = IGS(EG_STR_ID_PLAYER_SCREEN_MODE);
    p_g_fc_lbl.text = IGS(EG_STR_ID_PLAYER_FINGER_DRAG);
}

- (void)scale_to_fit_iPad
{
    CGRect rect = {0};
    CGPoint pnt = {0};
    rect.origin.x = 0.f;
    rect.origin.y = 0.f;
    rect.size.width = 1024.f;
    rect.size.height = 768.f;
    pnt.x = 512.f;
    pnt.y = 384.f;
    self.bounds = rect;
    self.center = pnt;
    self.frame = rect;
    //Controls
    //
    rect = p_loop_mode_btn.bounds;
    rect.origin.x = 1024.f - 200.f;
    rect.origin.y = 6.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_loop_mode_btn.bounds = rect;
    p_loop_mode_btn.center = pnt;
    //
    rect = p_play_btn.bounds;
    rect.origin.x = 10.f;
    rect.origin.y = 768.f - 120.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_play_btn.bounds = rect;
    p_play_btn.center = pnt;
    //
    rect = p_return_btn.bounds;
    rect.origin.x = 5.f;
    rect.origin.y = 5.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_return_btn.bounds = rect;
    p_return_btn.center = pnt;
    //
    rect = p_screen_mode_btn.bounds;
    rect.origin.x = 1024.f - 100.f;
    rect.origin.y = 10.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_screen_mode_btn.bounds = rect;
    p_screen_mode_btn.center = pnt;
    //
    rect = p_silent_icon.bounds;
    rect.origin.x = 1024.f - 100.f;
    rect.origin.y = 768.f - 120.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_silent_icon.bounds = rect;
    p_silent_icon.center = pnt;
    //
    rect = p_time_pos_bar.bounds;
    rect.origin.x = 120.f;
    rect.origin.y = 768.f - 133.f;
    rect.size.width = 1024.f - 120.f - 120.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_time_pos_bar.bounds = rect;
    p_time_pos_bar.center = pnt;
    //
    rect = p_time_pos_btn.bounds;
    rect.origin.x = 104.f;
    rect.origin.y = 768.f - 120.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_time_pos_btn.bounds = rect;
    p_time_pos_btn.center = pnt;
    //
    rect = p_vol_pos_bar.bounds;
    rect.origin.x = 1024.f - 120.f;
    rect.origin.y = 768.f - 120.f - 260.f;
    rect.size.height = 244.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_vol_pos_bar.bounds = rect;
    p_vol_pos_bar.center = pnt;
    //
    rect = p_vol_pos_btn.bounds;
    rect.origin.x = 1024.f - 120.f;
    rect.origin.y = 768.f - 120.f - rect.size.height;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_vol_pos_btn.bounds = rect;
    p_vol_pos_btn.center = pnt;
    //
    p_vol_btn.bounds = p_silent_icon.bounds;
    p_vol_btn.center = p_silent_icon.center;
    //
    rect = p_pause_state_icon.bounds;
    rect.origin.x = 512.f - 0.5f * rect.size.width;
    rect.origin.y = 384.f - 0.5f * rect.size.height;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_pause_state_icon.bounds = rect;
    p_pause_state_icon.center = pnt;
    //
    rect = p_finger_drag_icon.bounds;
    rect.origin.x = 80.f;
    rect.origin.y = 405.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_finger_drag_icon.bounds = rect;
    p_finger_drag_icon.center = pnt;
    //
    rect = p_bg_audio_logo.bounds;
    rect.origin.x = 512.f - 0.5f * rect.size.width;
    rect.origin.y = 384.f - 0.5f * rect.size.height;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_bg_audio_logo.bounds = rect;
    p_bg_audio_logo.center = pnt;

    
    //Labels
    //
    rect = p_movie_name_lbl.bounds;
    rect.origin.x = 100.f;
    rect.origin.y = 10.f;
    rect.size.width = 1024.f - 100.f - 250.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_movie_name_lbl.bounds = rect;
    p_movie_name_lbl.center = pnt;
    //
    rect = p_cur_time_lbl.bounds;
    rect.origin.x = 1024.f - 300.f;
    rect.origin.y = 768.f - 150.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_cur_time_lbl.bounds = rect;
    p_cur_time_lbl.center = pnt;
    //
    rect = p_cur_vol_lbl.bounds;
    rect.origin.x = 1024.f - 100.f;
    rect.origin.y = 768.f - 140.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_cur_vol_lbl.bounds = rect;
    p_cur_vol_lbl.center = pnt;
    
    
    
    
    //globalize labels
    //
    rect = p_g_ret_lbl.bounds;
    rect.origin.x = 10.f;
    rect.origin.y = 80.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_g_ret_lbl.bounds = rect;
    p_g_ret_lbl.center = pnt;
    //
    rect = p_g_lm_lbl.bounds;
    rect.origin.x = 1024.f - 200.f;
    rect.origin.y = 80.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_g_lm_lbl.bounds = rect;
    p_g_lm_lbl.center = pnt;
    //
    rect = p_g_sm_lbl.bounds;
    rect.origin.x = 1024.f - 100.f;
    rect.origin.y = 80.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_g_sm_lbl.bounds = rect;
    p_g_sm_lbl.center = pnt;
    //
    rect = p_g_ps_lbl.bounds;
    rect.origin.x = 40.f;
    rect.origin.y = 768.f - 140.f - rect.size.height;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_g_ps_lbl.bounds = rect;
    p_g_ps_lbl.center = pnt;
    //
    rect = p_g_fc_lbl.bounds;
    rect.origin.x = 60.f;
    rect.origin.y = 384.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    p_g_fc_lbl.bounds = rect;
    p_g_fc_lbl.center = pnt;
    
    if (p_time_pos_bar.image)
    {
        [p_time_pos_bar.image release];
    }
    p_time_pos_bar.image = [UIImage imageNamed:@RESOURCE_PATH_HD_TIME_LINE_BG];
    if (p_vol_pos_bar.image)
    {
        [p_vol_pos_bar.image release];
    }
    p_vol_pos_bar.image = [UIImage imageNamed:@RESOURCE_PATH_HD_VOLLINE];
    
}

- (void) hide_controls:(BOOL)enabled
{
    if (is_controls_hide == enabled)
    {
        return;
    }
    
    p_vol_pos_btn.hidden = enabled;
    p_vol_pos_bar.hidden = enabled;
    p_time_pos_btn.hidden = enabled;
    p_time_pos_bar.hidden = enabled;
    p_vol_btn.hidden = enabled;
    p_screen_mode_btn.hidden = enabled;
    p_return_btn.hidden = enabled;
    p_play_btn.hidden = enabled;
    
    p_finger_drag_icon.hidden = enabled;
    
#if IS_LITE_VERSION
    p_loop_mode_btn.hidden = YES;
    p_g_lm_lbl.hidden = YES;
#else
    p_loop_mode_btn.hidden = enabled;
    p_g_lm_lbl.hidden = enabled;
#endif
    if (enabled)
    {
        is_silent_icon_hide = p_silent_icon.hidden ? TRUE : FALSE;
        p_silent_icon.hidden = enabled;
    }
    else
    {
        {
            //set up loop mode reference
            E_LOOP_MODE lm = gf_get_ice_cfg()->loop_mode;
            switch (lm)
            {
                case E_LM_NO_LOOP:
                    p_loop_mode_btn.state = E_PLM_NO_LOOP;
                    [BGView get_Last_BG_view].configure_view.lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_NO_LOOP);
                    p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_NO_LOOP);
                    break;
                case E_LM_SINGLE_LOOP:
                    p_loop_mode_btn.state = E_PLM_SINGLE_LOOP;
                    [BGView get_Last_BG_view].configure_view.lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_SINGLE_LOOP);
                    p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_SINGLE_LOOP);
                    break;
                case E_LM_LIST_LOOP:
                    p_loop_mode_btn.state = E_PLM_LIST_LOOP;
                    [BGView get_Last_BG_view].configure_view.lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_LIST_LOOP);
                    p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_LIST_LOOP);
                    break;
                case E_LM_RANDOM:
                    p_loop_mode_btn.state = E_PLM_RANDOM;
                    [BGView get_Last_BG_view].configure_view.lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_RANDOM);
                    p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_RANDOM);
                    break;
                default:
                    break;
            }
        }
        p_silent_icon.hidden = is_silent_icon_hide ? TRUE : enabled;
    }
    
    //Labels
    p_movie_name_lbl.hidden = enabled;
    p_cur_time_lbl.hidden = enabled;
    p_cur_vol_lbl.hidden = enabled;
    //globalize labels
    p_g_ret_lbl.hidden = enabled;
    p_g_sm_lbl.hidden = enabled;
    p_g_ps_lbl.hidden = enabled;
    p_g_fc_lbl.hidden = enabled;
    
    is_controls_hide = enabled;
    
    ((PlayerViewController*)player_controller).enable_data_refresh_timer = !enabled;
}

- (void)Enable_All_Control:(BOOL)enabled
{
    p_vol_pos_btn.userInteractionEnabled = enabled;
    p_vol_pos_bar.userInteractionEnabled = enabled;
    p_time_pos_btn.userInteractionEnabled = enabled;
    p_time_pos_bar.userInteractionEnabled = enabled;
    p_vol_btn.userInteractionEnabled = enabled;
    p_screen_mode_btn.userInteractionEnabled = enabled;
    p_return_btn.userInteractionEnabled = enabled;
    p_play_btn.userInteractionEnabled = enabled;
    p_loop_mode_btn.userInteractionEnabled = enabled;
    p_silent_icon.userInteractionEnabled = enabled;
    p_finger_drag_icon.userInteractionEnabled = enabled;
    //Labels
    p_movie_name_lbl.userInteractionEnabled = enabled;
    p_cur_time_lbl.userInteractionEnabled = enabled;
    p_cur_vol_lbl.userInteractionEnabled = enabled;
    //globalize labels
    p_g_ret_lbl.userInteractionEnabled = enabled;
    p_g_lm_lbl.userInteractionEnabled = enabled;
    p_g_sm_lbl.userInteractionEnabled = enabled;
    p_g_ps_lbl.userInteractionEnabled = enabled;
    p_g_fc_lbl.userInteractionEnabled = enabled;
}

- (void) setHidden:(BOOL)enabled
{
    if (super.hidden == enabled)
    {
        return;
    }
    
    [self hide_controls:enabled];
    
	super.hidden = enabled;
}

- (void)destroy_SubViews
{
    //globalize labels
    RELEASE_ICE_VIEW(p_g_fc_lbl);
    RELEASE_ICE_VIEW(p_g_ps_lbl);
    RELEASE_ICE_VIEW(p_g_sm_lbl);
    RELEASE_ICE_VIEW(p_g_lm_lbl);
    RELEASE_ICE_VIEW(p_g_ret_lbl);
    //controls
    RELEASE_ICE_VIEW(p_cur_vol_lbl);
    RELEASE_ICE_VIEW(p_cur_time_lbl);
    RELEASE_ICE_VIEW(p_movie_name_lbl);
    RELEASE_ICE_VIEW(p_finger_drag_icon);
    RELEASE_ICE_VIEW(p_pause_state_icon);
    RELEASE_ICE_VIEW(p_vol_pos_btn);
    RELEASE_ICE_VIEW(p_vol_pos_bar);
    RELEASE_ICE_VIEW(p_time_pos_btn);
    RELEASE_ICE_VIEW(p_time_pos_bar);
    RELEASE_ICE_VIEW(p_silent_icon);
    RELEASE_ICE_VIEW(p_vol_btn);
    RELEASE_ICE_VIEW(p_screen_mode_btn);
    RELEASE_ICE_VIEW(p_return_btn);
    RELEASE_ICE_VIEW(p_play_btn);
    RELEASE_ICE_VIEW(p_loop_mode_btn);
    RELEASE_ICE_VIEW(p_bg_audio_logo);
    
    player_controller = NULL;
}

- (IBAction)On_Vol_Drag:(id)sender
{
    int a = 0;
    a &= 1;
}

- (void)On_Hide_All_Control
{
    [self hide_controls:YES];
}

- (void)On_Display_All_Control
{
    [self hide_controls:NO];
}

- (void)On_Finished_Moved_View
{
    self.hidden = FALSE;
}

- (void)On_Moved_View
{
    float offset = drag_end_pt.x - drag_begin_pt.x;
    BOOL forward = NO;
    if (offset < 0.f)
    {
        forward = YES;
    }
    else
    {
        forward = NO;
    }
    if ([((PlayerViewController*)player_controller) On_Change_File:forward])
    {
        if (offset < 0.f)
        {
            CATransition *animation = [CATransition animation];
            animation.delegate = self;
            animation.duration = 0.4f;
            animation.timingFunction = UIViewAnimationCurveEaseInOut;
            animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
            animation.type = kCATransitionReveal;//@"rippleEffect";//110
            animation.subtype = kCATransitionFromRight;//kCATransitionFromTop;
            //animation.endProgress = 1.0;
            //animation.removedOnCompletion = NO;
            
            //[self.layer addAnimation:animation forKey:@"animation"];
            [self.layer addAnimation:animation forKey:@"animation"];
            forward = YES;
        }
        else
        {
            CATransition *animation = [CATransition animation];
            animation.delegate = self;
            animation.duration = 0.4f;
            animation.timingFunction = UIViewAnimationCurveEaseInOut;
            animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
            animation.type = kCATransitionReveal;//@"rippleEffect";//110
            animation.subtype = kCATransitionFromLeft;//kCATransitionFromTop;
            //animation.endProgress = 1.0;
            //animation.removedOnCompletion = NO;
            
            //[self.layer addAnimation:animation forKey:@"animation"];
            [self.layer addAnimation:animation forKey:@"animation"];
            forward = NO;
        }
        self.hidden = YES;
        [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(On_Finished_Moved_View) userInfo:nil repeats:NO];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("B");
    CGPoint pt = [[touches anyObject] locationInView:self.superview];
    drag_begin_pt = pt;
    drag_begin_pos = self.bounds;
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("M");
    CGPoint pt = [[touches anyObject] locationInView:self.superview];
    CGRect rc = drag_begin_pos;
    if (ABS(pt.x - drag_begin_pt.x) > 80.f)
    {
        rc.origin.x -= (pt.x - drag_begin_pt.x)*1/2;
        [self pause_video_render];
        self.bounds = rc;
    }
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("C");
    CGPoint pt = [[touches anyObject] locationInView:self.superview];
    drag_end_pt = pt;
    drag_end_pos = self.bounds;
    if (ABS(drag_end_pt.x - drag_begin_pt.x) > FINGER_DRAG_CONFIRM_VAL/*(drag_end_pos.size.width / 2.f)*/)
    {
        [self On_Moved_View];
    }
    else
    {
        if (is_controls_hide)
        {
            [self On_Display_All_Control];
        }
        else
        {
            if ([Player_Model get_State] == E_PS_PAUSED || [Player_Model get_State] == E_PS_PLAYING) {
                [self On_Hide_All_Control];
            }
        }
    }
    self.bounds = drag_begin_pos;
    [self resume_video_render];
    [super touchesCancelled:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("E");
    CGPoint pt = [[touches anyObject] locationInView:self.superview];
    drag_end_pt = pt;
    drag_end_pos = self.bounds;
    if (ABS(drag_end_pt.x - drag_begin_pt.x) > FINGER_DRAG_CONFIRM_VAL/*(drag_end_pos.size.width / 2.f)*/)
    {
        [self On_Moved_View];
    }
    else
    {
        if (is_controls_hide)
        {
            [self On_Display_All_Control];
        }
        else
        {
            if ([Player_Model get_State] == E_PS_PAUSED || [Player_Model get_State] == E_PS_PLAYING) {
                [self On_Hide_All_Control];
            }
        }
    }
    self.bounds = drag_begin_pos;
    [self resume_video_render];
    [super touchesEnded:touches withEvent:event];
}

@end
